(display "\n========================================\n")
(define (make-frame origin edge1 edge2)
    (list origin edge1 edge2))

(define (origin-frame frame)
    (car frame))

(define (edge1-frame frame)
    (cadr frame))

(define (edge2-frame frame)
    (caddr frame))

(define (make-vect x y)
    (list x y))

(define (xcor-vect v)
    (car v))

(define (ycor-vect v)
    (cadr v))

(define (add-vect v1 v2)
    (make-vect (+ (xcor-vect v1) (xcor-vect v2))
               (+ (ycor-vect v1) (ycor-vect v2))))

(define (sub-vect v1 v2)
    (make-vect (- (xcor-vect v1) (xcor-vect v2))
               (- (ycor-vect v1) (ycor-vect v2))))

(define (scale-vect s v)
    (make-vect (* s (xcor-vect v))
               (* s (ycor-vect v))))

(define (frame-coord-map frame)
    (lambda (v)
        (add-vect
            (origin-frame frame)
            (add-vect
                (scale-vect (xcor-vect v)
                            (edge1-frame frame))
                (scale-vect (ycor-vect v)
                            (edge2-frame frame))))))

(define (transform-painter painter origin corner1 corner2)
    (lambda (frame)
            (let ((m (frame-coord-map frame)))
                (let ((new-origin (m origin)))
                    (painter
                        (make-frame new-origin
                                    (sub-vect (m corner1) new-origin)
                                    (sub-vect (m corner2) new-origin)))))))

(define (beside painter1 painter2)
    (let ((left-painter
            (transform-painter painter1
                               (make-vect 0 0)
                               (make-vect 0.5 0)
                               (make-vect 1 0)))
          (right-painter
            (transform-painter painter2
                               (make-vect 0.5 0)
                               (make-vect 1 0)
                               (make-vect 0.5 1))))
        (lambda (frame)
                (left-painter frame)
                (right-painter frame))))

(define (below painter1 painter2)
    (let ((top-painter
            (transform-painter painter2
                               (make-vect 0 0.5)
                               (make-vect 1 0.5)
                               (make-vect 0 1)))
          (bottom-painter
            (transform-painter painter1
                               (make-vect 0 0)
                               (make-vect 1 0)
                               (make-vect 0 0.5))))
        (lambda (frame)
                (top-painter frame)
                (bottom-painter frame))))

(define (flip-horiz painter)
    (transform-painter painter
                       (make-vect 1 0)
                       (make-vect 0 0)
                       (make-vect 1 1)))

(define (rotate90 painter)
    (transform-painter painter
                       (make-vect 0 1)
                       (make-vect 0 0)
                       (make-vect 1 1)))

(define (rotate270 painter)
    (transform-painter painter
                       (make-vect 1 0)
                       (make-vect 1 1)
                       (make-vect 0 0)))

(define (below painter1 painter2)
    (lambda (frame)
        ((flip-horiz
            (rotate270
                (beside
                    (rotate90 (flip-horiz painter1))
                    (rotate90 (flip-horiz painter2)))))
        frame)))
(display "\n========================================\n")